<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class VoucherArise_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_voucher';
	}
        
        public function _getListPaymentMethod(){
            $getListPaymentMethod = $this->voucherArise_model->getListPaymentMethod();
            unset($getListPaymentMethod[2]);
            unset($getListPaymentMethod[4]);
            return $getListPaymentMethod;
        }


        public function export($arr_status){
            $this->db
		->select("tbl.id,tbl.*,bc.name,usr.username,bvd.payment_confirm AS approve")
                ->from($this->table_name.' AS tbl')
                ->join('book_contacts AS bc','bc.customerID = tbl.customerID')
                ->join('users AS usr','usr.id=tbl.owner')
                ->join('book_voucher_debts AS bvd','bvd.voucher_id=tbl.id','LEFT')
                ->where('tbl.payment_method',6);
            
            if(!in_array($this->session->userdata['group_id'],array(1,79))){
                $this->db->where('tbl.owner',$this->session->userdata['user_id']);
            }

            if($arr_status->customersName){
                $this->db->where('bc.name REGEXP ("'.$arr_status->customersName.'")');
            }

            if($arr_status->v_booking_place_code){
                $this->db->where('tbl.booking_place_code LIKE "%'.$arr_status->v_booking_place_code.'%"');
            }

            if($arr_status->v_payment_method){
                $this->db->where('tbl.payment_method ',$arr_status->v_payment_method);
            }
            
            if($arr_status->h_beginTime != ''){
                    $this->db->where('c.lastupdated BETWEEN '.$arr_status->h_beginTime. ' AND '.$arr_status->h_endTime);
                }

           $query = $this->db->get();
            //echo $this->db->last_query();exit;
            return $query->result();
        }
        
        public function getListBookingTypes(){
            return $this->booking_types;
        }

        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'sumNet',
						'label' => $this->lang->line('sumNet'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'sumSale',
						'label' => $this->lang->line('sumSale'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'payment_company',
						'label' => $this->lang->line('payment_company'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name'  => 'button',
						'width' => '5%',
						'sort'  => FALSE,
						'label' =>  $right['add']==TRUE?'<div class="btn-group">
						<a style="width: 82px;" href="'.site_url('auth/voucher/index/add').'" class="btn btn btn-success">'.$this->lang->line('create').'</a>
						</div>':"",
						'searchoptions' => false
				)
		);
	}
	
	public function json_data($controller, $right){
            
                if($this->session->userdata('v_voucherArise')){
                    $arr_status = json_decode($this->session->userdata('v_voucherArise'));
                }
            
		$this->datatables
		->select("tbl.id,tbl.*,bc.name,usr.username,bvd.payment_confirm AS approve")
                ->from($this->table_name.' AS tbl')
//                ->join('book_payment_method AS method','method.id = tbl.payment_method')
                ->join('book_contacts AS bc','bc.customerID = tbl.customerID')
                ->join('users AS usr','usr.id=tbl.owner')
                ->join('book_voucher_debts AS bvd','bvd.voucher_id=tbl.id','LEFT')
                ->where('tbl.payment_method',6);
                
                if(!in_array($this->session->userdata['group_id'],array(1,79))){
                    $this->datatables->where('tbl.owner',$this->session->userdata['user_id']);
                }
                
                if(isset($arr_status) && $arr_status->v_customerName != ''){
                    $this->datatables->where('bc.name REGEXP ("'.$arr_status->v_customerName.'")');
                }
                
                if(isset($arr_status) && $arr_status->v_booking_place_code != ''){
                    $this->datatables->where('tbl.booking_place_code LIKE "%'.$arr_status->v_booking_place_code.'%"');
                }
                
                if(isset($arr_status) && $arr_status->v_payment_method != ''){
                    $this->datatables->where('tbl.payment_method ',$arr_status->v_payment_method);
                }
                
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                
                $this->session->unset_userdata('v_voucherArise');
                
		foreach($datatables['aaData'] as $item){
                        $booking_place_code = json_decode($item['booking_place_code']);
                        $airline            = json_decode($item['airline']);
                        
                        $pnr                            = '';
                        foreach($booking_place_code as $key => $vals){
                            if($key < count($booking_place_code)-1){
                                $etc = ', ';
                            } else {
                                $etc = '';
                            }
                            $pnr .= $vals.'-'.$airline[$key].$etc;
                        }
			$ouput['aaData'][] = array(
					$item['id'],
                                        $item['name'],
                                        ($pnr)?$pnr:'',
                                        $this->bookinglib->my_number_format($item['net_fare'],2, ',', ','),
                                        $this->bookinglib->my_number_format($item['sale_fare'],2, ',', ','),
                                        ($item['payment_confirm'] == 1)?'Đã thanh toán':'Chưa thanh toán',
                                        $item['username'],
                                        date("d-m-Y H:i:s",strtotime($item['lastupdated'])),
                                        $this->add_button_public($controller, $right, $item)
			);
		}
	
		return json_encode($ouput);
	}

}